<?php
class NguoidungController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
     
    }

    public function capnhatAction()
    {
        $user_id = $this->getRequest()->getParam('id');
        $this->view->title = 'Cập nhật người dùng';
        $formCapnhat = new Form_NguoiDungForm($user_id);
        
        $formCapnhat->setMethod('post');
        $formCapnhat->setAction('/zfMHB/public/nguoidung/capnhat/');
        
        $formCapnhat->addElement('submit', 'submit', array('label' => 'Lưu'));
        $formCapnhat->addElement('hidden', 'id');
        $nguoidungMapper = Library_Model_NguoiDungMapper::getInstance();
        $this->view->form = $formCapnhat;
        
        if($this->getRequest()->isPost()){
        	if($formCapnhat->isValid($_POST)){
        		$data = $formCapnhat->getValues();
        		
        		$id 					= $data['id'];
        		$tennd 				= $data['tennguoidung'];
        		$email 				= $data['email'];
        		$tendn 				= $data['tendangnhap'];
        		$matkhau 		= md5($data['matkhau']);
        		$khoa 				= '0';
        		$quyenhan 		= 'users';
        		
        		$nguoidungObj = new Library_Model_NguoiDung();        		
        		$nguoidungObj->setId($id);
        		$nguoidungObj->setTennd($tennd);
        		$nguoidungObj->setEmail($email);
        		$nguoidungObj->setTendangnhap($tendn);
        		$nguoidungObj->setMatkhau($matkhau);
        		$nguoidungObj->setKhoa($khoa);
        		$nguoidungObj->setQuyenhan($quyenhan);
        		
        		$nguoidung = $nguoidungMapper->save($nguoidungObj);
        		
        		$this->_redirect('/admin/nguoidung/index/');
        	}
        } else {
        		$user = $nguoidungMapper->searchById($user_id);
        		
        		$formCapnhat->getElement('id')->setValue($user->getId());
        		$formCapnhat->getElement('tennguoidung')->setValue($user->getTennd());
        		$formCapnhat->getElement('email')->setValue($user->getEmail());
        		$formCapnhat->getElement('tendangnhap')->setValue($user->getTendangnhap());
        		$formCapnhat->getElement('matkhau')->setValue($user->getMatkhau());
        		$formCapnhat->getElement('khoa')->setValue($user->getKhoa());
        		$formCapnhat->getElement('quyenhan')->setValue($user->getQuyenhan());
        	}
        
    }

   /*  public function thaydoiAction(){
    	$this->view->title = "Thay đổi thông tin cá nhân";
    	$formThaydoi = new Form_NguoiDungForm();
    	$formThaydoi->setName('Thay đổi thông tin cá nhân');
    	
    	$formThaydoi->removeElement('tendangnhap');
    	$formThaydoi->removeElement('matkhau');
    	//$formThaydoi->removeElement('captcha');
    	$formThaydoi->setMethod('post');
    	$formThaydoi->setAction('/zfMHB/public/nguoidung/thaydoi');
    	$formThaydoi->addElement('hidden','nguoidung_id');
    	$formThaydoi->addElement('submit','submit',array('label'=>'Cập nhật'));
    	$nguoidungMapper = Library_Model_NguoiDungMapper::getInstance();
    	//$custMapper = Store_Model_CustomerMapper::getInstance();
    	$this->view->form = $formThaydoi;
    	$nguoidung_id = Zend_Auth::getInstance()->getIdentity()->nguoidung_id;
    	$nguoidung = $nguoidungMapper->searchById($nguoidung_id);
    	if($this->getRequest()->isPost())
    	{
    		if($formThaydoi->isValid($_POST))
    		{
    			$data = $formThaydoi->getValues();
    	
    			$id = $data['nguoidung_id'];
    			$email = $data['email'];
    			$tennguoidung = $data['tennguoidung'];
    			
    			$checkEmailId = $nguoidungMapper->checkEmailId($email,$id);
    	
    			if(!$checkEmailId)  {    	
    				$nguoidungObj = new Library_Model_NguoiDung();    				
    				$tendangnhap = $nguoidung->getUsername();
    				$matkhau = $nguoidung->getPassword();
    				$quyenhan = $nguoidung->getQuyenhan();
    				
    					
    				$nguoidungObj->setId($id);
    				$nguoidungObj->setUsername($tendangnhap);
    				$nguoidungObj->setPassword($matkhau);
    				$nguoidungObj->setEmail($email);
    				$nguoidungObj->setQuyenhan($quyenhan);
    					    	
    				$updateUser = $nguoidungMapper->save($nguoidungObj);
    				
    				$this->_redirect('/nguoidung/thaydoi');
    			}
    			else {
    				echo "Email đã tồn tại";
    			}
    	
    		}
    	}
    	////////////////////////////////////////////////
    	/* else {
    		$customers = $user->getCustomer();
    			
    		$formThaydoi->getElement('user_id')->setValue($user->getId());
    		$formThaydoi->getElement('email')->setValue($user->getEmail());
    		foreach($customers as $customer)
    		{
    	
    			$formThaydoi->getElement('firstname')->setValue($customer->getFirstname());
    			$formThaydoi->getElement('lastname')->setValue($customer->getLastname());
    			$formThaydoi->getElement('address')->setValue($customer->getAddress());
    			$formThaydoi->getElement('city')->setValue($customer->getCity());
    			$formThaydoi->getElement('phone')->setValue($customer->getPhone());
    			$formThaydoi->getElement('birth_date')->setValue($customer->getBirthdate());
    		}
    			
    	} 
    } */
    public function validateformAction()
    {
        $user_id = $this->getRequest()->getParam('id');
        $this->_helper->viewRenderer()->setNoRender();
        $this->_helper->getHelper('layout')->disableLayout();
        $form = new Admin_Form_ThemNguoiDungForm();
        $form->isValid($this->_getAllParams());
        $json = $form->getMessages();
        header('Content-type: application/json');
        echo Zend_Json::encode($json);
    }
}